package IO流;

import java.io.File;

/**
 *使用面向对象；统计文件夹大小
 *
 * */
public class File03 {
    private long len;
    private String dirPath;
    private File file;
    private int fileSize;
    private int dirSize;
    public File03(String dirPath){
        this.dirPath = dirPath;
        this.file = new File(dirPath);
        count(this.file);
    }

    public int getFileSize() {
        return fileSize;
    }

    public int getDirSize() {
        return dirSize;
    }

    public long getLen() {
        return len;
    }

    public static void main(String[] args) {
        File03 src = new File03("C:\\huangzhi\\workspace\\javaStudy");
        System.out.println("文件夹长度为"+src.getLen()+"文件数量为"+(src.getFileSize()-1)+"文件夹数量为"+(src.getDirSize()-1));
    }
    //统计大小
    private void count(File src) {
        //获取大小

        if(null != src||src.exists()){
            //是文件则计算大小并叠加
            if (src.isFile()){
                this.fileSize++;
                len += src.length();
            }else {//进入其子孙级，递归其大小
                this.dirSize++;
                for (File f : src.listFiles()){
                    count(f);
                }
            }
        }
    }


}
